home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / monitory / lav / timer.c < prev    next >
C/C++ Source or Header  |  1993-06-12  |  3KB  |  149 lines

  1. /**************************************
  2. *  TIMER.C  08/04/90
  3. *  Written by Timm Martin
  4. *  This source code is public domain.
  5. ***************************************/
  6.  
  7. #include <devices/timer.h>
  8. #include <exec/ports.h>
  9. #include <exec/types.h>
  10. #include <clib/exec_protos.h>
  11.  
  12. #include "timer.h"
  13.  
  14. /********************
  15. *  SHARED VARIABLES
  16. *********************/
  17.  
  18. long    timer_error = 1;
  19. struct timerequest timer_req;
  20. struct MsgPort *timer_port = NULL;    /* GLOBAL */
  21.  
  22. /***************
  23. *  TIMER ABORT
  24. ****************/
  25.  
  26. /*
  27.  * This function cancels an existing time request.
  28.  */
  29.  
  30. void
  31. timer_abort(void)
  32. {
  33.     AbortIO(&timer_req.tr_node);
  34.     Wait(1L << timer_port->mp_SigBit);
  35.     GetMsg(timer_port);
  36. }
  37.  
  38. /***************
  39. *  TIMER CLOSE
  40. ****************/
  41.  
  42. /*
  43.  * This function closes the timer device and deletes the timer port.
  44.  */
  45.  
  46. void
  47. timer_close(void)
  48. {
  49.     if (!timer_error) {
  50.         CloseDevice((struct IORequest *) & timer_req);
  51.         timer_error = NULL;
  52.     }
  53.     if (timer_port) {
  54.         DeletePort(timer_port);
  55.         timer_port = NULL;
  56.     }
  57. }
  58.  
  59. /**************
  60. *  TIMER OPEN
  61. ***************/
  62.  
  63. /*
  64.  * This function opens the timer device and initializes it.
  65.  */
  66.  
  67. BOOL
  68. timer_open(void)
  69. {
  70.     if (!(timer_port = (struct MsgPort *)CreatePort(NULL, 0L)) ||
  71.         (timer_error = OpenDevice(TIMERNAME, UNIT_VBLANK,
  72.                 (struct IORequest *) & timer_req, NULL)))
  73.         return (0);
  74.  
  75.     timer_req.tr_node.io_Message.mn_ReplyPort = timer_port;
  76.     timer_req.tr_node.io_Command = TR_ADDREQUEST;
  77.     timer_req.tr_node.io_Flags = 0;
  78.     return (1);
  79. }
  80.  
  81. /***************
  82. *  TIMER START
  83. ****************/
  84.  
  85. /*
  86.  * This function issues a request to the timer device to notify the
  87.  * program in the specified number of microseconds.  This function does
  88.  * not wait for a reply from the timer device.
  89.  */
  90.  
  91. void
  92. timer_start(long micros)
  93. {
  94.     long    secs;
  95.  
  96.     secs = micros / MICROS_PER_SEC;
  97.     micros %= MICROS_PER_SEC;
  98.  
  99.     timer_req.tr_time.tv_secs = secs;
  100.     timer_req.tr_time.tv_micro = micros;
  101.     SendIO(&timer_req.tr_node);
  102. }
  103.  
  104. /**************
  105. *  TIMER TEST
  106. ***************/
  107.  
  108. /*
  109.  * This function returns whether the last timer request has been
  110.  * satisfied.
  111.  */
  112.  
  113. BOOL
  114. timer_test(void)
  115. {
  116.     return (GetMsg(timer_port) != NULL);
  117. }
  118.  
  119. /**************
  120. *  TIMER WAIT
  121. ***************/
  122.  
  123. /*
  124.  * This function waits for the specified number of microseconds.
  125.  */
  126.  
  127. #define MICROS_PER_SEC 1000000L
  128.  
  129. void
  130. timer_wait(long micros)
  131. {
  132.     long    secs;
  133.  
  134.     /* a bug in Kickstart v1.3 requires this check */
  135.     if (micros < 2)
  136.         return;
  137.  
  138.     secs = micros / MICROS_PER_SEC;
  139.     micros %= MICROS_PER_SEC;
  140.  
  141.     timer_req.tr_time.tv_secs = secs;
  142.     timer_req.tr_time.tv_micro = micros;
  143.     SendIO(&timer_req.tr_node);
  144.  
  145.     /* wait until time is up */
  146.     Wait(1L << timer_port->mp_SigBit);
  147.     GetMsg(timer_port);
  148. }
  149.